var but = getButton();

var box = document.createElement("div");
box.style.backgroundColor = "#000000";
box.style.color = "#FFFFFF";
box.style.width = "100%";
box.style.height = "100%";
box.style.position = "absolute";
box.style.top = "0";
box.style.left = "0";
box.style.textAlign = "center";
box.style.fontSize = "12px";
box.style.fontFamily = "'Lucida Grande', Arial, Helvetica, sans-serif";
box.style.zIndex = "10000";
document.body.appendChild(box);

var logo = document.createElement("div");
logo.style.paddingTop = "50px";
logo.align = 'center';
box.appendChild(logo);
logo.innerHTML = "<img src='http://www.cuevana.tv/img/logo.png' />";

var box2 = document.createElement("div");
box2.id = "box2"
box2.style.paddingTop = "50px";
box.appendChild(box2);
document.body.style.overflow = "hidden";

if (document.getElementById("dwltmr") && document.getElementById("dwltmr").innerHTML.match(/You are currently downloading/i)) {
	box2.innerHTML = "Ya estás descargando un archivo de Hotfile.<br />No puedes descargar más de 1 archivo en simultáneo sin una cuenta premium en Hotfile.com (<a href='http://www.hotfile.com/premium.html' target='_blank'>comprar una cuenta premium en Hotfile</a>).";
} else if (but && but.value.match(/REGULAR DOWNLOAD/gi)) {
	var pag = document.body.innerHTML;
	if (pag.match(/Wrong code/i)) {
		box2.innerHTML = "El código ingresado no es correcto. Vuelve a intentarlo.<br />";
	}
	regetButton();
	
	but.addEventListener("click", clickBut,false);
} else if (getElementByClass("click_download")) {
	getURL();
} else if (document.getElementById("recaptcha_widget_div")) {
	loadCaptcha();
} else {
	var pag = document.body.innerHTML;
	if (pag.match(/This file is either removed due to copyright claim or is deleted by the uploader/i)) {
		box2.innerHTML = "El archivo ha sido eliminado de Hotfile.";
	}
}

// Funciones
function clickBut(e) {
	but.value = "Cargando...";
	but.removeEventListener("click", clickBut,false);
	var t = document.getElementById("dwltmr");
	if (t.innerHTML.match(/You reached your hourly traffic limit/i)) {
		checkTime();
		document.getElementById("dwltxt").addEventListener("DOMSubtreeModified",checkTime,false);
		document.getElementById("freebut").removeEventListener("DOMSubtreeModified",regetButton,false);
	} else {
		t.addEventListener("DOMSubtreeModified",goTimer,false);
	}
}
function loadCaptcha() {
	var forms = document.getElementsByTagName("form");
	var f = forms[forms.length-1];
	box2.appendChild(f);
	document.getElementById("box2").style.paddingTop = "80px";
	var but2 = getElementByClass("but");
	but2.value = "Enviar captcha";
	var t = getElementByClass("tbl").getElementsByTagName("tr");
	t[0].firstChild.style.paddingLeft = "0px";
	t[t.length-1].innerHTML = "";
}
function goTimer() {
	var s = parseInt(document.getElementById("dwltmr").firstChild.firstChild.innerHTML.replace(/([^0-9])+/gi,''));
	box2.innerHTML = "Por favor, espera "+s+" segundos";
	if (s==0) {
		setTimeout("getLink()",100);
	}
}
function checkTime() {
	var d = document.getElementById("dwltxt");
	var dw = d.firstChild.getElementsByTagName("span")[0].innerHTML;
	var s = parseInt(dw.replace(/([^0-9])+/gi,''));
	if (s>0) {
		var q = dw.match(/minutes/i) ? "minutos" : "segundos";
		box2.innerHTML = "Has llegado al límite de descarga en Hotfile. Puedes volver a descargar en "+s+" "+q+" o puedes <a href='http://www.hotfile.com/premium.html' target='_blank'>comprar una cuenta premium en Hotfile</a> para descargar sin límites.";
	} else {
		d.removeEventListener("DOMSubtreeModified",checkTime,false);
	}
}
function getLink() {
	if (!document.getElementById("captcha")) {
		box2.innerHTML = "Cargando...";
		setTimeout("getLink()",100);
	}
}
function getURL() {
	if (getElementByClass("click_download")) {
		var a = getElementByClass("click_download"), hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).replace(/amp;/gi,'').replace(/lang=en/i,'');
		var h = hashes.split('&'), hash, vars = [];
		for(var i=0;i<h.length;i++){
	        hash = h[i].split('=');
	        vars.push(hash[0]);
	        vars[hash[0]] = hash[1];
	    }
		var id = vars['id'], tipo = (vars['tipo']=="s") ? 'series' : 'peliculas', epi = (tipo=='series') ? '/episodio' : '';
		window.location = 'http://www.cuevana.tv/'+tipo+'/'+id+epi+'/play/?megaurl='+a.href+'&count=20&'+hashes;
	}
}
function getElementByClass(theClass) {
	var allHTMLTags=document.getElementsByTagName("*");
	for (i=0; i<allHTMLTags.length; i++) {
		if (allHTMLTags[i].className==theClass) {
			return allHTMLTags[i];
		}
	}
	return false;
}
function getButton() {
	var form = document.getElementsByName("f");
	if (form) {
		form=form[0];
		var inputs = document.getElementsByTagName("input");
		var intot = inputs.length;
		if ((intot-2)>=0) {
			return inputs[intot-2];
		} else {
			return false;
		}
	} else {
		return false;
	}
}
function regetButton() {
	var but = getButton();
	box2.innerHTML = "";
	box2.appendChild(but);
	
	but.value = "Click aquí para cargar el video";
	but.className = '';
	but.style.background = "#333";
	but.style.fontWeight = "bold";
	but.style.padding = "3px 7px";
	but.style.color = "#FFFFFF";
	but.style.border = "0px";
	but.style.width = "auto";
	but.style.height = "auto";
	but.style.fontFamily = "'Lucida Grande', Arial, Helvetica, sans-serif";
	
	but.addEventListener("click", clickBut,false);
}